package cn.mrlong.recordandplay;

import android.annotation.SuppressLint;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Handler;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class PlayerUtil implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, MediaPlayer.OnErrorListener {


    public interface MediaPlayerStatuListener {
        void currentStatu(String status);

        void progress(double i);

        void progress2(long i);
    }

    private MediaPlayer mediaPlayer;
    private Timer mTimer;
    private MediaPlayerStatuListener mediaPlayerStatuListener;

    private TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            try {
                if (mediaPlayer == null) {
                    return;
                } else if (mediaPlayer.isPlaying()) {
                    handler.sendEmptyMessage(0);
                }
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("mediaPlayer==>", "" + e.getMessage());
            }
        }
    };

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 0:
                    int position = mediaPlayer.getCurrentPosition();
                    int duration = mediaPlayer.getDuration();
                    if (duration > 0) {
                        double pos = (double) position / duration;
                        mediaPlayerStatuListener.progress(pos);
                        Log.e("PlayerUtil == >", "" + pos);
                    }
                    break;
                case 1:
                    mediaPlayerStatuListener.currentStatu(STATU_CACHING);
                    break;
                case 2:
                    mediaPlayerStatuListener.currentStatu(STATU_PLAYING);
                    break;
            }
        }
    };

    public final static String STATU_CACHING = "缓存中";
    public final static String STATU_PLAYING = "播放中";

    public void play() {
        if (null != mediaPlayer)
            mediaPlayer.start();
    }

    public void playUrl(String url, MediaPlayerStatuListener mediaPlayerStatuListener) {
        this.mediaPlayerStatuListener = mediaPlayerStatuListener;
        try {
            if (null == mediaPlayer)
                mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setOnBufferingUpdateListener(this);
            mediaPlayer.setOnPreparedListener(this);
            mediaPlayer.setOnCompletionListener(this);
            mediaPlayer.setOnErrorListener(this);
            if (null != mTimer) {
                mTimer = null;
            }
            mTimer = new Timer();
            mTimer.schedule(timerTask, 0, 100);
            handler.sendEmptyMessage(1);
            mediaPlayer.reset();
            mediaPlayer.setDataSource(url);
            mediaPlayer.prepare();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("mediaPlayer==>", "" + e.getMessage());
        }
    }

    public void pause() {
        if (null != mediaPlayer)
            mediaPlayer.pause();
        Log.e("mediaPlayer==>", "pause");
    }

    public void stop() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mTimer.cancel();
            mTimer = null;
            mediaPlayer.release();
            mediaPlayer = null;
        }
        Log.e("mediaPlayer==>", "stop");
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        handler.sendEmptyMessage(2);
        mp.start();
        Log.e("mediaPlayer==>", "onPrepared");
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        mediaPlayerStatuListener.progress(0);
        mediaPlayerStatuListener.progress2(0);
        mediaPlayerStatuListener.currentStatu("");
        stop();
        Log.e("mediaPlayer==>", "onCompletion");
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        if (null != mediaPlayer) {
            mediaPlayer.reset();
        }
        Log.e("mediaPlayer==>", "onError");
        return false;
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        mediaPlayerStatuListener.progress2(percent);
    }

}
